/**
 * Created by karim on 2017/1/17.
 */
$(".leftNav a:nth-child(5)").css("background","#1a63be")
$('#rightMain_title').html('用户信息');	//修改标题
var userNo =null;
if(window.location.search!=""){
	userNo = getUrlParam("id");
}
$("#editUserBtn").click(function(){
	window.location.href='editUser.html?id='+userNo;
})
$("#addBtn").click(function(){
	window.location.href='editUser.html';
})
if(userNo=="6YOd"){
	$("#editUsername").attr("disabled",false);
	$("#editTruename").attr("disabled",false);
	$("#editUsername").css("background","#fff");
	$("#editTruename").css("background","#fff");
}else{
	$.ajax({	
		url:"user/findUser.html",
		type:"post",
		data:{
			id:userNo
		},
		 success:function(result){
			 result=JSON.parse(result)//解析字符串
			if(result.result!=0||result.data.length==0){//判断是否获取成功
				setTimeout(function(){
					swal("查询失败！", "查询失败！","error");
					return;
				},0);
			}
			 var gid = result.data.gid==1?"管理员":"普通用户";
			 var myA = '<table cellspacing="1" cellpadding="0"><tbody cellspacing="1" cellpadding="0" style="padding:0;margin:0;width:100%">'+
			 '<tr><td>用户名称</td><td>'+result.data[0].username+'</td></tr>'+
			 '<tr><td>实名信息</td><td>'+result.data[0].truename+'</td></tr>'+
			 '<tr><td>绑定手机</td><td>'+result.data[0].mobile+'</td></tr>'+
			 '<tr><td>用户权限</td><td>'+gid+'</td></tr>'+
			 '<tr><td>时间权限</td><td>24小时</td></tr>'+
			 '<tr><td>在线时间</td><td>1分58秒</td></tr>'+
			 '</tbody></table>';
			 $("#userVal").prepend(myA); //将用户信息表格插入#userVal顶部
			 $("#editUserVal").attr("name",result.data[0].id);//#editUserVal的name为该用户id
			 /*将用户信息加入编辑栏显示*/
			 $("#editUsername").val(result.data[0].username);
			 $("#editUserPhone").val(result.data[0].mobile);
			 $("#editUserPsw").val(result.data[0].password);
			 $("#editTruename").val(result.data[0].truename);
		 },
		 error:function(err){
			 console.log(err);
		 }
	});

}

$("#editUserSave").click(function(){/*点击保存用户信息*/
	/*获取input框中的输入的用户信息*/
	var userName = $("#editUsername").val();
	var trueName = $("#editTruename").val();
	var loginName = $("#editUsername").val();
	var userId = $("#editUserVal").attr("name");
	var phone = $("#editUserPhone").val();
	var psw = $("#editUserPsw").val();
	/*判断输入的用户信息是否为空*/
	var pattern = /^1[34578]\d{9}$/;    
	if(psw==""){
		setTimeout(function(){
    		swal("密码为空!");
    		$("div h2").css("fong-size","20px")
    	},0);
		return false;
	};
	if(userName==""){
		setTimeout(function(){
			swal("用户名为空!");
			$("div h2").css("fong-size","20px")
		},0);
		return false;
	};
	if(trueName==""){
		setTimeout(function(){
			swal("真实姓名为空!");
			$("div h2").css("fong-size","20px")
		},0);
		return false;
	};
	if(phone==""){
		setTimeout(function(){
			swal("手机号码为空!");
			$("div h2").css("fong-size","20px")
		},0);
		return false;
	};
	$.ajax({/*将新编辑好的用户信息发送到服务器端保存*/
		url:"user/updateUser.html",
		type:"post",
		data:{"id":userId,"password":psw,username:userName,truename:trueName,mobile:phone},
	 success:function(result){
		 result=JSON.parse(result);
		 if(result.result==0){
			 setTimeout(function(){
 				swal("保存成功！", "点击ok返回用户页!", "success");
 				$(".confirm").unbind();
 				 $(".confirm").click(function(){
						 window.location.href='user.html'
					 });
       		},0);
		 }else{
				setTimeout(function(){
             		swal(" 保存失败！",result.message, "error");
             	},0);
		 };
		 }
	 })
})

	$("#editUserBack").click(function(){
		window.location.href='user.html';
	});

    function setFocus(e){
	    e.scrollIntoView(false);
	};

$("input").focus(function () {
	/*设置input获取焦点时，将下一行的input滚动到课件范围内*/
	if(this.id!="editUserPsw"){/*判断是否为最后一个输入框*/
    	if($(this).parent().nextAll("p").children("input").length!=0){/*判断是否有下一个输入框*/
    		 var myId = $(this).parent().nextAll("p").children("input")[0].id;
        	 myInput = document.getElementById(myId);
    	}else{ 
    		myInput = document.getElementById(this.id);
    	};
    }else{
    	var myId =$(this).parent().nextAll("div").children("input")[0].id;
    	myInput = document.getElementById(myId);
    };
    setTop = setInterval('setFocus(myInput)',10);
})

    $("input").blur(function(){
        clearInterval(setTop);
    });
